package com.example.servlet;

import com.example.dao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/UserForgetPassword")
public class UserForgetPassword extends HttpServlet {
    private UserDao userDao = new UserDao();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username2");
        String password = request.getParameter("password2");
        String repeatPassword = request.getParameter("repeatPassword");
        if(!password.equals(repeatPassword)){
            request.setAttribute("msg","两次密码不一致");
            request.getRequestDispatcher("forgotPassword.jsp").forward(request,response);
            return;
        }
        if(userDao.checkUsername(username)==false){
            request.setAttribute("msg","用户名不存在");
            request.getRequestDispatcher("forgotPassword.jsp").forward(request,response);
            return;
        }
        int affectedRows = userDao.forgetPassword(username, password);
        if(affectedRows==0){
            request.setAttribute("msg","修改密码失败,请重试！");
            request.getRequestDispatcher("forgotPassword.jsp").forward(request,response);
            return;
        }
        request.setAttribute("msg","密码修改成功!");
        request.getRequestDispatcher("login.jsp").forward(request,response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
